package com.huawei.hwid.d;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hwid.c.a;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.h;
import com.huawei.hwid.core.d.j;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.datatype.SiteDefaultInfo;
import com.huawei.hwid.core.datatype.SiteListInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.xmlpull.v1.XmlPullParserException;

public final class b {
    private static ArrayList<String> c = new ArrayList();
    private static b d = null;
    public ArrayList<SiteListInfo> a = new ArrayList();
    private SiteDefaultInfo b = new SiteDefaultInfo();

    private b() {
    }

    public static synchronized b a() {
        b bVar;
        synchronized (b.class) {
            if (d == null) {
                d = new b();
            }
            bVar = d;
        }
        return bVar;
    }

    public synchronized String a(Context context, int i) {
        Object c;
        boolean z = true;
        synchronized (this) {
            e.b("SiteCountryDataManager", "Start getASServerUrlBySiteID.", true);
            HwAccount b = a.a(context).b();
            String str = "SiteCountryDataManager";
            StringBuilder append = new StringBuilder().append("hwAccount == null:");
            if (b != null) {
                z = false;
            }
            e.b(str, append.append(z).toString(), true);
            e.b("SiteCountryDataManager", "hwAccount getASDomain:" + (b != null ? b.o() : "empty"), true);
            if (b == null || TextUtils.isEmpty(b.o())) {
                e.b("SiteCountryDataManager", "getASServerUrlBySiteID getHwAccount was empty siteID:" + i, true);
                c = c(context, i);
            } else {
                e.b("SiteCountryDataManager", "getASServerUrlBySiteID getHwAccount was not empty siteID:" + i, true);
                c = "https://" + b.o() + "/AccountServer";
            }
            if (TextUtils.isEmpty(c)) {
                e.b("SiteCountryDataManager", "AS server url is null from current cache and file.", true);
            }
        }
        return c;
    }

    public synchronized String b(Context context, int i) {
        Object d;
        boolean z = true;
        synchronized (this) {
            e.b("SiteCountryDataManager", "Start getCASServerUrlBySiteID", true);
            HwAccount b = a.a(context).b();
            String str = "SiteCountryDataManager";
            StringBuilder append = new StringBuilder().append("hwAccount == null:");
            if (b != null) {
                z = false;
            }
            e.b(str, append.append(z).toString(), true);
            e.b("SiteCountryDataManager", "hwAccount getCASDomain:" + (b != null ? b.p() : "empty"), true);
            if (b == null || TextUtils.isEmpty(b.p())) {
                e.b("SiteCountryDataManager", "getCASServerUrlBySiteID getHwAccount was empty siteID:" + i, true);
                d = d(context, i);
            } else {
                e.b("SiteCountryDataManager", "getCASServerUrlBySiteID getHwAccount was not empty siteID:" + i, true);
                d = "https://" + b.p();
            }
            if (TextUtils.isEmpty(d)) {
                e.b("SiteCountryDataManager", "CAS server url is null from current cache and file.", true);
            }
        }
        return d;
    }

    private synchronized void c(Context context) {
        if (this.a.isEmpty()) {
            e.b("SiteCountryDataManager", "inner update.", true);
            a(context);
        }
    }

    public synchronized void a(final Context context) {
        e.b("SiteCountryDataManager", "start countDownLatch innerinit", true);
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        new Thread(new Runnable(this) {
            final /* synthetic */ b c;

            public void run() {
                e.b("SiteCountryDataManager", "start thread innerinit", true);
                try {
                    this.c.a.clear();
                    b.c.clear();
                    j.a(context, this.c.a, this.c.b, b.c);
                } catch (XmlPullParserException e) {
                    e.b("SiteCountryDataManager", "XmlPullParserException = " + e.getClass().getSimpleName(), true);
                } catch (IOException e2) {
                    e.b("SiteCountryDataManager", "IOException = " + e2.getClass().getSimpleName(), true);
                } catch (Exception e3) {
                    e.b("SiteCountryDataManager", "Exception = " + e3.getClass().getSimpleName(), true);
                } finally {
                    e.b("SiteCountryDataManager", "finish thread innerinit", true);
                    countDownLatch.countDown();
                }
            }
        }).start();
        boolean z = false;
        try {
            z = countDownLatch.await(3, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.b("SiteCountryDataManager", "InterruptedException", true);
        }
        e.b("SiteCountryDataManager", "end countDownLatch innerinit awaitValue:" + z, true);
    }

    public synchronized ArrayList<String> b(Context context) {
        c(context);
        e.b("SiteCountryDataManager", "get white list from file.", true);
        return c;
    }

    private String c(Context context, int i) {
        e.b("SiteCountryDataManager", "getASServerDomainBySiteID start", true);
        String a = a(context, i, 0);
        if (!TextUtils.isEmpty(a)) {
            a = "https://" + a + "/AccountServer";
        }
        e.b("SiteCountryDataManager", "accountServerDomain::=" + a, false);
        return a;
    }

    private String d(Context context, int i) {
        e.b("SiteCountryDataManager", "getCASServerDomainBySiteID start", true);
        String a = a(context, i, 1);
        if (!TextUtils.isEmpty(a)) {
            a = "https://" + a;
        }
        try {
            h.a(context, null);
        } catch (IOException e) {
            e.d("SiteCountryDataManager", "Error occured when download global in get CAS url.", true);
        }
        e.b("SiteCountryDataManager", "casServerDomain::=" + a, false);
        return a;
    }

    public String a(Context context, int i, int i2) {
        String b;
        e.b("SiteCountryDataManager", "Start refresh accout info cache and file, siteID: " + i, true);
        String str = "";
        String str2 = "";
        c(context);
        if (i <= 0) {
            i = com.huawei.hwid.core.d.b.a(context, "");
        }
        if (i > 0) {
            Iterator it = this.a.iterator();
            while (it.hasNext()) {
                SiteListInfo siteListInfo = (SiteListInfo) it.next();
                if (siteListInfo != null && siteListInfo.a() == i) {
                    str2 = siteListInfo.c();
                    b = siteListInfo.b();
                    e.b("SiteCountryDataManager", "asDomain From Global::= " + b, false);
                    e.b("SiteCountryDataManager", "casDomain From Global::= " + str2, false);
                    break;
                }
            }
            b = str2;
            str2 = str;
        } else {
            e.b("SiteCountryDataManager", "get serverDomainBySiteID default.", true);
            str2 = this.b.b();
            b = this.b.a();
            e.b("SiteCountryDataManager", "asDomain From Default::= " + b, false);
            e.b("SiteCountryDataManager", "casDomain From Default::= " + str2, false);
        }
        HwAccount b2 = a.a(context).b();
        if (b2 != null) {
            e.b("SiteCountryDataManager", "No accout info from memcache and local file to refresh.", true);
            b2.o(str2);
            b2.n(b);
            if (2 == i2) {
                b2.q(String.valueOf(System.currentTimeMillis()));
            }
            a.a(context).a(context, b2);
        }
        if (i2 == 0) {
            return b;
        }
        if (1 == i2) {
            return str2;
        }
        return "";
    }
}
